#include common_scripts\utility;
#include maps\mp\_utility;
#include maps\mp\gametypes\_hud_util;
#include z_mod\_utility;


init()
{
    level thread onPlayerConnected();
}

onPlayerConnected()
{
    for(;;) {
        level waittill("connected", player);
        
        player sayAll("-> " + player maps\mp\gametypes\_persistence::statGet("cardIcon"));
        
        player.z_Energy = 0;
        player.z_EnergyMax = 0;
        player.z_MenuIsSelectingSoul = false;
        player.z_SoulCurrent = -1;
        player.z_SoulExp = 0;
        player.z_SoulWanted = 0;
        
        player thread onPlayerDied();
        player thread onPlayerJoinedSpectators();
        player thread onPlayerJoinedTeam();
        player thread onPlayerSpawned();
        
        player notifyOnPlayerCommand("zkey_actionslot_1", "+actionslot 1");
        player notifyOnPlayerCommand("zkey_actionslot_2", "+actionslot 2");
        player notifyOnPlayerCommand("zkey_actionslot_3", "+actionslot 3");
        player notifyOnPlayerCommand("zkey_actionslot_4", "+actionslot 4");
        player notifyOnPlayerCommand("zkey_activate", "+activate"); // меню - выбрать
        player notifyOnPlayerCommand("zkey_back", "+back"); // меню - вниз
        player notifyOnPlayerCommand("zkey_forward", "+forward"); // меню - вверх
    }
}

onPlayerDied()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("death");
        // <...>
    }
}

onPlayerJoinedSpectators()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("joined_spectators");
        
        // разблокировка души при выходе в зрители
        if(self.z_SoulCurrent != -1) {
            level.z_Souls[self.z_SoulCurrent].player = undefined;
            self.z_SoulCurrent = -1;
        }
    }
}

onPlayerJoinedTeam()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("joined_team");
        
        // разблокировка души при смене команды
        if(self.z_SoulCurrent != -1) {
            level.z_Souls[self.z_SoulCurrent].player = undefined;
            self.z_SoulCurrent = -1;
        }
    }
}

onPlayerSpawned()
{
    self endon("disconnect");
    
    for(;;) {
        self waittill("spawned_player");
        
        setDvar("cg_drawCrosshair", 1);
        
        self.z_MenuIsSelectingSoul = false;
        
        if(self.z_SoulCurrent == -1) {
            if(!getDvarInt("z_RandomSouls")) {
                self thread z_mod\_menu::startSoulSelecting();
            } else {
                self.z_SoulCurrent = z_mod\_souls::getRandomSoul();
                self.z_SoulExp = 0;
                
                level.z_Souls[self.z_SoulCurrent].player = self;
                
                self z_mod\_souls::giveSoul();
            }
        }
        
        self thread playerEnergy();
    }
}

playerEnergy()
{
    self endon("death");
    self endon("disconnect");
    
    if(self.z_SoulCurrent == -1)
        self waittill("changed_kit");
    
    self.z_EnergyMax = level.z_Souls[self.z_SoulCurrent].energy;
    self.z_Energy = self.z_EnergyMax;
    
    while(true) {
        if(self.z_Energy < self.z_EnergyMax)
            self.z_Energy++;
        
        wait(1.0);
    }
}


